package com.xc.quickstart.manager.wechat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xc.quickstart.common.model.ApiResponse;
import com.xc.quickstart.common.util.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Slf4j
@Component
public class WeChatGroupRobotManager {


    public static final String AutoDistributeWebHook = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f5dbd763-c68e-4d46-94c5-5e06925f7803";

    public static final String testUser = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f914b972-c631-4b79-9c61-01c07743984b";

    public static ApiResponse send(String webHook, String content) {

        if (StringUtils.isEmpty(content)) {
            log.warn("[WeChatManager]发送内容为空");
        }

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msgtype","text");
        JSONObject text = new JSONObject();
        text.put("content",content);
        jsonObject.put("text",text);

        String response = RestTemplateUtil.postJson(webHook,headers, JSON.toJSONString(jsonObject));
        return JSON.parseObject(response, ApiResponse.class);
    }

}
